package com.example.ffepeg.Ikanalyzer;

import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

public class IkanalyzerUtils {
    /**
     *  分词
     * @param keyword 需要分词的文本
     * @return
     */
    public static List<String> splitKeyWord(String keyword) {

        try {
            ArrayList<String> result = new ArrayList<>();
            // 创建一个reader对象
            StringReader reader = new StringReader(keyword);
            // 创建一个分词对象
            IKSegmenter ikSegmenter = new IKSegmenter(reader, true);
            Lexeme next = ikSegmenter.next();
            while ( next != null ) {
                // 获取分词的结果
                String lexemeText = next.getLexemeText();
                // 如果分词结果是 "ai" 则转为 "AI"
                if ("ai".equalsIgnoreCase(lexemeText)) {
                    lexemeText = "AI";
                }
                result.add(next.getLexemeText());
                next = ikSegmenter.next();
            }
            return result;
        }catch (Exception e){
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * 需要对一些数据文test本进行分词
     * useSmart  --> false
     * [需要, 要对, 一些, 一, 些, 数据, 文, test, 本, 进行, 分词]
     *
     * 需要对一些数据文test本进行分词terminal
     * useSmart  --> true
     * [需, 要对, 一些, 数据, 文, test, 本, 进行, 分词, terminal]
     */

    private static boolean isChinese(String str) {
        for (char c : str.toCharArray()) {
            if (Character.UnicodeBlock.of(c) == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        //String str = "ai眺望着缓first time北流头看万千染过颜色订单";
        //String str = "智者千虑必有一失，愚者千着必有一得";
        //String str = "Taking a morning walk has numerous benefits for both body and mind. As the sun rises, stepping outdoors allows your body to absorb vitamin D, essential for bone health and immune system support. The fresh air and natural surroundings can also significantly uplift your mood, setting a positive tone for the rest of the day. Physically, walking is a low-impact exercise that helps improve circulation, strengthens muscles, and can contribute to maintaining a healthy weight. ";
       // String str = "春节,作为中国最重要的传统节日,标志着农历新年的开始,是家庭团聚和庆祝的时刻.每当这个时候test,大街小巷都充满了喜庆的气氛,红灯笼高挂,春联贴在门边,寓意着驱邪迎福.家家户户都会进行大扫除,以崭新的面貌迎接新的一年.";

        String str = "=易AI春节,作为中国最重要的传统节日,标志着农历新年的开始";
        Integer i = estimateDigit(str);
        System.out.println("全部数据为: "+ i);

       /* System.out.println("这个字符串全是英文的是与否: " + isEnglish(str));
        if (isEnglish(str)){
            String englished = englishSymbols(str);
            Integer i = estimateDigit(englished);
            System.out.println("数据为: " + i);
        }*/
    }


    private static Integer estimateDigit(String text){
        List<String> list = splitKeyWord(text);
        System.out.println("list数据为: " + list);
        int sum = 0;
        int fun = 0;
        for (String item : list) {
            if (isChinese(item)) {  //是否包含阿拉伯数字
                System.err.println(item);   //中文
                int length = item.length();
                sum += length;
            } else {
                System.out.println(item);    //英文
                boolean englishUsingCharacter = isEnglishUsingCharacter(item); //只有英文
                if (englishUsingCharacter){
                    if (item.equals("AI") || item.equals("aI") || item.equals("Ai") || item.equals("ai")){
                        sum += 2;
                    }else {
                        sum += 1;
                    }
                }

                String regex = "^(?=.*[0-9])(?=.*[a-zA-Z]).+$"; //包含数字与英文
                boolean containsDigit = item.matches(regex);
                if (containsDigit) {
                    String alpha = item.replaceAll("[^a-zA-Z]", "");  // 只保留字母
                    String numeric = item.replaceAll("[^0-9]", "");   // 只保留数字
                    int length = numeric.length() + 1;
                    fun += length;
                }
                boolean matches = item.matches("^[0-9]+$");
                if (matches){
                    int length = item.length();
                    fun += length;
                }
            }
        }
        System.out.println("值为1: " + sum);
        System.out.println("值为2: " + fun);
        System.out.println("值为3: " + (sum+fun));
        return (sum+fun);
    }

    public static boolean isEnglishUsingCharacter(String input) {
        if (input == null || input.isEmpty()) {
            return false;
        }
        for (char ch : input.toCharArray()) {
            if (!Character.isLetter(ch) || !((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))) {
                return false;
            }
        }
        return true;
    }


    //------------------------------------------------------------------------------------------------------------------
    //区分中文和英文字符
    public static boolean isEnglish(String str) {
        char[] charArray = str.toCharArray(); // 将字符串转换为字符数组
        for (char ch : charArray) {
            // 检查字符是否不在ASCII范围内,或者是空格或常用标点符号  <包括大小写字母、数字和一些符号>
            if (!(ch >= 0 && ch <= 127) && !Character.isWhitespace(ch) && !isCommonPunctuation(ch)) {
                return false;
            }
        }
        return true;
    }

    private static boolean isCommonPunctuation(char ch) {
        // 定义你认为的常见标点符号.  这可以扩展
        return ",;:.!?-()[]{}".indexOf(ch) >= 0;
    }

    private static String englishSymbols(String text){
        // 使用正则表达式替换所有匹配的符号为空字符串
        String cleanedText = text.replaceAll("[,\\-\\.\\?\\!\\@#\\$%\\^&\\*()\\$\\${};:'\"<>/\\\\|_~`]", "");
        System.out.println(cleanedText);
        return cleanedText;
    }





}
